/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.model.Catalog;
import com.click.app.model.Category;
import com.click.app.model.LabelValue;
import com.click.app.model.ProductType;
import com.click.app.service.CategoryManager;
import com.click.app.service.GenericManager;
import com.click.app.service.ProductTypeManager;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 *
 * @author orgale
 */
public class CategoryAction extends BaseAction {

    private CategoryManager categoryManager;
    private GenericManager<ProductType, Long> productTypeManager;
    private GenericManager<Catalog, Long> catalogManager;
    private List categorys;
    private Category category;
    private Long id;
    private String name;
    private Set<String> productTypeOpions;
    private Set<String> productTypeOptionsPicked;
    private List<LabelValue> productTypeListLeft;
    private List<LabelValue> productTypeListRight;
    private Set<LabelValue> productTypeListLeftV;
    private Set<LabelValue> productTypeListRightV;

    public void setId(Long id) {
        this.id = id;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public List<String> getProductNames() {
        List<String> names = new ArrayList<String>();
        List<ProductType> ps = productTypeManager.getAll();

        for (ProductType p : ps) {
            names.add(p.getName());
        }

        return names;
    }
    /*
    public boolean productsFree(Category dis) {
    boolean resp = true;

    List<Product> mats = productManager.getAll();
    for (Product m : mats) {
    for (Category d : m.getListCategorys()) {
    if (d.equals(dis)) {
    resp = false;

    }
    }
    }

    return resp;
    }
     */

    public String delete() {
        System.out.println("remove category");
        for (Catalog c : catalogManager.getAll()) {
            if (c.getCategorys().contains(categoryManager.get(category.getId()))) {
                c.getCategorys().remove(categoryManager.get(category.getId()));
            }
            catalogManager.save(c);
        }
        categoryManager.remove(new Long(category.getId()));
        saveMessage(getText("category.deleted"));
        System.out.println("catalogs remaining: " + catalogManager.getAll().size());
        return SUCCESS;
    }

    public boolean parentsFree(ProductType p) {
        boolean resp = true;
        for (Category pt : categoryManager.getAll()) {
            for (ProductType pe : pt.getProductTypes()) {
                if (pe.getId().equals(p.getId())) {
                    resp = false;
                }
            }
        }
        return resp;
    }

    public List<LabelValue> getProductTypesNamesAndValues() {
        List<LabelValue> namesAndValues = new ArrayList<LabelValue>();
        List<ProductType> ps = productTypeManager.getAll();

        for (ProductType p : ps) {
            if (parentsFree(p)) {
                namesAndValues.add(new LabelValue(p.getNameLang(), p.getId().toString()));
            }
        }
        return namesAndValues;

    }

    public LabelValue getPos(List<LabelValue> lp, ProductType pt) {
        for (LabelValue lv : lp) {
            if (pt.getName().matches(lv.getLabel()) && (pt.getId().toString().matches(lv.getValue()))) {
                return lv;
            }
        }
        return null;
    }

    public String edit() {
        if (id != null) {
            category = categoryManager.get(id);
            productTypeListLeft = getProductTypesNamesAndValues();
            productTypeListRight = new ArrayList<LabelValue>();
            for (ProductType p : category.getProductTypes()) {
                productTypeListRight.add(new LabelValue(p.getNameLang(), p.getId().toString()));
                productTypeListLeft.remove(getPos(getProductTypesNamesAndValues(), p));
            }

            getSession().setAttribute("productTypes", category.getProductTypes());
        } else {
            category = new Category();
            Date n = new Date();
            category.setDateCategory(n);
            productTypeListLeft = getProductTypesNamesAndValues();
            productTypeListRight = new ArrayList<LabelValue>();
        }

        return SUCCESS;
    }

    public boolean validationPasses() {
        boolean vp = true;
        if (category.getName() == null || category.getName().matches("")) {
            errorMessage("Required field, name.");
            vp = false;
        }
        if (category.getNameEs() == null || category.getNameEs().matches("")) {
            errorMessage("Required field, name (es).");
            vp = false;
        }

        System.out.println("validation result:" + vp);
        return vp;
    }

    public String sar() throws Exception {
        System.out.println("test");
        return SUCCESS;
    }

    public String save() throws Exception {
        System.out.println("Saving category...");
        if (cancel != null) {
            return CANCEL;
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (category.getId() == null);

        category.setProductTypes(new ArrayList<ProductType>());

        if (productTypeOptionsPicked != null) {
            for (String s : getProductTypeOptionsPicked()) {
                ProductType pt = productTypeManager.get(new Long(Integer.parseInt(s)));
                category.getProductTypes().add(pt);

            }
        }

        if (validationPasses()) {
            category = categoryManager.save(category);
        /*  if (getProductTypeOptionsPicked() != null) {
        for (String s : getProductTypeOptionsPicked()) {
        ProductType pt = productTypeManager.get(new Long(Integer.parseInt(s)));
        category.getProductTypes().add(pt);
        pt.setCategory(category);
        productTypeManager.save(pt);
        }
        }*/

        } else {
            edit();
            return ERROR;
        }

        String key = (isNew) ? "category.added" : "category.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }

    public void setCategoryManager(CategoryManager categoryManager) {
        this.categoryManager = categoryManager;
    }

    public void setProductTypeManager(GenericManager<ProductType, Long> productTypeManager) {
        this.productTypeManager = productTypeManager;
    }

    public void setCatalogManager(GenericManager<Catalog, Long> catalogManager) {
        this.catalogManager = catalogManager;
    }

    public List getCategorys() {
        return categorys;
    }

    public String list() {
        categorys = categoryManager.getAll();
        return SUCCESS;
    }

    public String helpList() {
        return SUCCESS;
    }

    public String help() {
        return SUCCESS;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    public String saveCategoryDescription() {
        System.out.println("d: " + getRequest().getAttribute("category"));

        return SUCCESS;
    }
    private String categoryName;
    private String categoryDescription;

    /**
     * @return the categoryName
     */
    public String getCategoryName() {
        return categoryName;
    }

    /**
     * @param categoryName the categoryName to set
     */
    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    /**
     * @return the categoryDescription
     */
    public String getCategoryDescription() {
        return categoryDescription;
    }

    /**
     * @param categoryDescription the categoryDescription to set
     */
    public void setCategoryDescription(String categoryDescription) {
        this.categoryDescription = categoryDescription;
    }

    public List<Catalog> getCatalogs() {
        return catalogManager.getAll();
    }

    /**
     * @return the productTypeOpions
     */
    public Set<String> getProductTypeOpions() {
        return productTypeOpions;
    }

    /**
     * @param productTypeOpions the productTypeOpions to set
     */
    public void setProductTypeOpions(Set<String> productTypeOpions) {
        this.productTypeOpions = productTypeOpions;
    }

    /**
     * @return the productTypeOptionsPicked
     */
    public Set<String> getProductTypeOptionsPicked() {
        return productTypeOptionsPicked;
    }

    /**
     * @param productTypeOptionsPicked the productTypeOptionsPicked to set
     */
    public void setProductTypeOptionsPicked(Set<String> productTypeOptionsPicked) {
        this.productTypeOptionsPicked = productTypeOptionsPicked;
    }

    /**
     * @return the productTypeListLeft
     */
    public List<LabelValue> getProductTypeListLeft() {
        return productTypeListLeft;
    }

    /**
     * @param productTypeListLeft the productTypeListLeft to set
     */
    public void setProductTypeListLeft(List<LabelValue> productTypeListLeft) {
        this.productTypeListLeft = productTypeListLeft;
    }

    /**
     * @return the productTypeListRight
     */
    public List<LabelValue> getProductTypeListRight() {
        return productTypeListRight;
    }

    /**
     * @param productTypeListRight the productTypeListRight to set
     */
    public void setProductTypeListRight(List<LabelValue> productTypeListRight) {
        this.productTypeListRight = productTypeListRight;
    }

    /**
     * @return the productTypeListLeftV
     */
    public Set<LabelValue> getProductTypeListLeftV() {
        return productTypeListLeftV;
    }

    /**
     * @param productTypeListLeftV the productTypeListLeftV to set
     */
    public void setProductTypeListLeftV(Set<LabelValue> productTypeListLeftV) {
        this.productTypeListLeftV = productTypeListLeftV;
    }

    /**
     * @return the productTypeListRightV
     */
    public Set<LabelValue> getProductTypeListRightV() {
        return productTypeListRightV;
    }

    /**
     * @param productTypeListRightV the productTypeListRightV to set
     */
    public void setProductTypeListRightV(Set<LabelValue> productTypeListRightV) {
        this.productTypeListRightV = productTypeListRightV;
    }
}